#! /bin/bash
# filename : chrony-server_edit.sh

set -e

CHRONY_CONFIG='/etc/chrony.conf'
NTP_SERVER=( 
    0.cn.pool.ntp.org
    1.cn.pool.ntp.org
    2.cn.pool.ntp.org
    ntp.ntsc.ac.cn
    ntp1.aliyun.com
)
ALLOW_HOST=( 0.0.0.0/0 )

sed -ri "s/^\s*(pool|server)/#&/g" "${CHRONY_CONFIG}"

for i in "${NTP_SERVER[@]}"; do
    echo "server ${i} iburst" |tee -a "${CHRONY_CONFIG}"
done

for i in "${ALLOW_HOST[@]}"; do
    output="allow ${i}"
    if grep -q "^\s*${output}" "${CHRONY_CONFIG}"; then
        continue
    fi
    echo "allow ${i}" |tee -a "${CHRONY_CONFIG}"
done

systemctl restart chronyd.service

echo "chronyd status is : $(systemctl is-active chronyd.service)"
